<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Info as InfoModel;
use app\admin\model\Money as MoneyModel;
use app\admin\model\Excels as ExcelsModel;
use think\Db;
use think\Request;

class Api extends Controller
{
    //座位信息接口
    public function getSelectSeat(Request $request) {
        //获取post传递的数据
        $startTime = $request->post('starttime');
        $endTime = $request->post('endtime');

        //动态获取器withAttr，查询选择时间段内选座数据
        $condition = [
            'start_time'=>function ($value){
                return intval(substr($value,11,2));
            },
            'end_time'=>function ($value) {
                return intval(substr($value, 11, 2));
            }
        ];

        //查询当天的数据
        $today = date('Y-m-d');
        $query = InfoModel::field(['start_time','end_time','seat_number'])->whereLike('start_time',$today.'%')->withAttr($condition)->select();
        //在时间段内的数据
        $res1 = $query->where('start_time','>=',$startTime)->where('end_time','<=',$endTime);
        //start_time<开始时间<end_time的数据
        $res2 = $query->where('start_time','<',$startTime)->where('end_time','>',$startTime);
        //start_time<结束时间<end_time的数据
        $res3 = $query->where('start_time','<',$endTime)->where('end_time','>',$endTime);

        //把查询出来的数据添加到一个数组中
        $data = array();
        foreach ($res1 as $key=>$value) {
            $data[] = $value;
        }
        foreach ($res2 as $key=>$value) {
            $data[] = $value;
        }
        foreach ($res3 as $key=>$value) {
            $data[] = $value;
        }

        return json_encode($data);
    }

    //保存用户选择座位的信息接口
    public function saveSeatInfo(Request $request) {
        //获取post传递的数据
        $username    = $request->post('username');
        $start_time  = $request->post('start_time');
        $end_time    = $request->post('end_time');
        $time_count  = $end_time-$start_time;
        $seat_number = $request->post('seat_number');

        $data = [
            'username'         =>  $username,
            'start_time'       =>  $start_time,
            'end_time'         =>  $end_time,
            'time_count'       =>  $time_count,
            'seat_number'      =>  $seat_number,
        ];
        $info = new InfoModel();
        //新增数据
        $result = $info->save($data);

        $items = [
            'result' => $result,
        ];
        return json_encode($items);
    }

    //判断是否重复选座接口
    public function repeat(Request $request) {
        //获取post传递的数据
        $username    = $request->post('username');
        $start_time  = $request->post('start_time');
        $end_time    = $request->post('end_time');

        //动态获取器withAttr，查询选择时间段内选座数据
        $condition = [
            'start_time'=>function ($value){
                return intval(substr($value,11,2));
            },
            'end_time'=>function ($value) {
                return intval(substr($value, 11, 2));
            }
        ];
        //查询当天的数据
        $today = date('Y-m-d');
        //查找当前时间段内是否有重复选座数据
        $query = InfoModel::field('username,start_time,end_time')->whereLike('start_time',$today.'%')->withAttr($condition)->where('username',$username)->select();
        $one1 = $query->where('start_time','>=',$start_time)->where('end_time','<=',$end_time);
        $one2 = $query->where('start_time','<',$start_time)->where('end_time','>',$start_time);
        $one3 = $query->where('start_time','<',$end_time)->where('end_time','>',$end_time);

        $ones = array();
        foreach ($one1 as $k=>$v) {
            $ones[] = $v;
        }
        foreach ($one2 as $k=>$v) {
            $ones[] = $v;
        }
        foreach ($one3 as $k=>$v) {
            $ones[] = $v;
        }

        return json_encode($ones);
    }

    //我的记录接口
    public function record(Request $request) {
        //获取post传递的数据
        $username = $request->post('username');

        //查询记录，包括软删除的数据
        $res = InfoModel::withTrashed()->field('username,start_time,end_time,seat_number')->where('username',$username)->select();

        $datas = array();
        foreach ($res as $key=>$value) {
            $datas[] = $value;
        }

        return json_encode($datas);
    }

    //余额接口
    public function getMoney(Request $request) {
        //接收传值
        $wx_name = $request->post('nickName');
//        $wx_name = '哟嚯嚯嚯';

        $data = MoneyModel::field('money_count,cost_count')->where('wx_name',$wx_name)->find();
        $data['remain_money'] = $data['money_count']-$data['cost_count'];

        return json_encode($data);
    }

    //花费细明接口
    public function getCost(Request $request) {
        //接收传值
        $wx_name = $request->post('nickName');
//        $wx_name = 'tian';

        //根据微信名查询money表中姓名字段的值
        $username = MoneyModel::field('username')->where('wx_name',$wx_name)->value('username');
        $data = ExcelsModel::field('username,start_time,end_time,time_count,cost')->where('username',$username)->select();

        return json_encode($data);
    }
}
